package com.zhuhjay.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.eduservice.entity.CrmBanner;
import com.zhuhjay.eduservice.service.CrmBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-07
 */
@Api(description = "后台管理员Banner接口")
@RestController
@RequestMapping("/educms/banneradmin")

public class BannerAdminController {

    @Resource
    private CrmBannerService bannerService;

    @ApiOperation(value = "分页查询Banner")
    @GetMapping("pageBanner/{current}/{limit}")
    public R getAllBanner(@PathVariable Long current, @PathVariable Long limit) {
        Page<CrmBanner> page = new Page<>(current, limit);
        bannerService.page(page, null);
        return R.ok().data("items", page.getRecords())
                .data("total", page.getTotal());
    }

    @ApiOperation(value = "添加Banner")
    @PostMapping("addBanner")
    @CacheEvict(value = "BANNER", allEntries = true)
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PostMapping("updateBanner")
    @CacheEvict(value = "BANNER", allEntries = true)
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("removeBanner/{id}")
    @CacheEvict(value = "BANNER", allEntries = true)
    public R removeBanner(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }

}

